package ${packageName}.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ${packageName}.dao.${table.entityName}DAO;
import ${packageName}.entity.${table.entityName};
import ${packageName}.service.${table.entityName}Service;
import com.keta.foundation.util.dwz.Page;
import com.keta.foundation.util.dwz.PageUtil;

@Service
@Transactional
public class ${table.entityName}ServiceImpl implements ${table.entityName}Service {

	@Autowired
	private ${table.entityName}DAO ${table.instanceName}DAO;
	
	@Override
	public ${table.entityName} get(${table.pk.javaType} ${table.pk.fieldName}) {
		return ${table.instanceName}DAO.findOne(${table.pk.fieldName});
	}
	

	@Override
	public long get${table.entityName}Count() {
		return ${table.instanceName}DAO.count();
	}

	@Override
	public void saveOrUpdate(${table.entityName} ${table.instanceName}) {
		${table.instanceName}DAO.save(${table.instanceName});
	}
	
	@Override
	public List<${table.entityName}> getAll() {
		return ${table.instanceName}DAO.findAll();
	}

	@Override
	public List<${table.entityName}> getAll(Page page) {
		org.springframework.data.domain.Page<${table.entityName}> springPage = ${table.instanceName}DAO.findAll(PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		return springPage.getContent();
	}

	@Override
	public List<${table.entityName}> getAll(Specification<${table.entityName}> specification, Page page) {
		org.springframework.data.domain.Page<${table.entityName}> springPage = ${table.instanceName}DAO.findAll(specification,
				PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		return springPage.getContent();
	}

	@Override
	public void delete(${table.pk.javaType} ${table.pk.fieldName}) {
		${table.instanceName}DAO.delete(${table.pk.fieldName});
	}
	
	@Override
	public void delete(${table.entityName} ${table.instanceName}) {
		${table.instanceName}DAO.delete(${table.instanceName});
	}

}
